import 'dart:ui' as ui;
import 'dart:math' as Math;
import 'package:flutter_elf/config/component_index.dart';

class AntForestPage extends BaseWidget{
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return AntForestPageState();
  }
}

class AntForestPageState extends BaseWidgetState<AntForestPage> with TickerProviderStateMixin{
  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
    return null;
  }
  List<WaterBean> waters;
  AnimationController _animationController;
  WaterBean selectWater;
  double treeCenterX = 125;
  double treeCenterY = 250;
  Color textColor = Colors.white;
  AnimationController _floatAnimationController;
  @override
  void onCreate() {
    // TODO: implement onCreate
    super.onCreate();
    waters = [
      WaterBean(textColor,0,14,0,0,4.2,Colors.green,30),
      WaterBean(textColor,1,14,150,30,7.1,Colors.blue,40),
      WaterBean(textColor,2,14,80,120,12.3,Colors.redAccent,20),
    ];
    _animationController = AnimationController(vsync: this,duration: Duration(milliseconds: 2000),)..addListener((){
      if(selectWater != null){
        double x = selectWater.xPosition;
        double y = selectWater.yPosition;
        selectWater.xPosition = x + ((treeCenterX - x) * _animationController.value);
        selectWater.yPosition = y + ((treeCenterY - y) * _animationController.value);
        selectWater.textColor = selectWater.textColor.withOpacity(1 - _animationController.value);
        selectWater.color = selectWater.color.withOpacity(1 - _animationController.value);
      }
    })..addStatusListener((AnimationStatus status){
      if(status == AnimationStatus.completed){
        _animationController.reset();
        globalPrint(selectWater.xPosition.toString());
        globalPrint(selectWater.yPosition.toString());
        _floatAnimationController.forward();
      }
    });

    _floatAnimationController = AnimationController(vsync: this,duration: Duration(seconds: 2))..addListener((){
      double value ;
      if(_floatAnimationController.value < 0.5){
        value = _floatAnimationController.value;
        for(WaterBean w in waters){
          w.yPosition = w.yPosition + 0.5 * value;
        }
      }else{
        value = _floatAnimationController.value - 0.5;
        for(WaterBean w in waters){
          w.yPosition = w.yPosition - 0.5 * value;
        }
      }
      setState(() {

      });
    })..addStatusListener((AnimationStatus status){
      if(status == AnimationStatus.completed){
        _floatAnimationController.reset();
        _floatAnimationController.forward();
      }
    });
    if(mounted)_floatAnimationController.forward();
  }
  TapDownDetails tapDownDetails;
  @override
  Widget buildView(BuildContext context) {
    // TODO: implement buildView
    return Container(
      height: px(600),
      width: 600,
      alignment: Alignment.center,
      child: GestureDetector(
        onTapDown: (TapDownDetails tapDownDetails){
          this.tapDownDetails = tapDownDetails;
        },
        onTapUp: (TapUpDetails tapUpDetails){
          if((tapUpDetails.localPosition.dx - tapDownDetails.localPosition.dx).abs() < 100){
            //有效点击
            double xClick = tapDownDetails.localPosition.dx;
            double yClick = tapDownDetails.localPosition.dy;
            globalPrint('点击的x' + xClick.toString());
            globalPrint('点击的y' + yClick.toString());
            List<WaterBean> w = waters.where((water){
              return (Math.pow((water.xPosition + water.radius - xClick), 2) + Math.pow((water.yPosition  + water.radius - yClick), 2)) < Math.pow(water.radius, 2);
            },).toList();
            if(w.isEmpty){
              showToast('empty');
            }else{
              for(var w in w){
                globalPrint(w.index.toString());
              }
              showToast('not empty');
              selectWater = w[w.length - 1 ];
              _floatAnimationController.stop(canceled: false);
              _animationController.animateTo(1,curve: Curves.linear,duration: Duration(milliseconds: 600));
            }


          }
        },
        child: AnimatedBuilder(
          builder: (context,child){
            return CustomPaint(
              size: Size(250,250),
              painter: WaterDrop(waters),
            );
          },
          animation: _animationController,
//          child: CustomPaint(
//            size: Size(px(500),px(500)),
//            painter: WaterDrop(waters),
//          ),
        ),
      ),
    );
  }


  @override
  void dispose() {
    // TODO: implement onDestroy
    _animationController.stop(canceled: true);
    _floatAnimationController.stop(canceled: true);
    _animationController?.dispose();
    _floatAnimationController?.dispose();
    // TODO: implement dispose
    super.dispose();
  }

}
class WaterDrop extends CustomPainter{
  List<WaterBean> waters;

  WaterDrop(this.waters);

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    drawWater(canvas,waters[0],size);
    drawWater(canvas,waters[1],size);
    drawWater(canvas,waters[2],size);

  }
  double textSize = 14;
  drawWater(Canvas canvas,WaterBean waterBean,Size size){
    Paint paint = Paint()
      ..color = waterBean.color
      ..strokeCap = StrokeCap.square
      ..strokeWidth = 5
      ..isAntiAlias = true;
    Offset offset = Offset(waterBean.xPosition + waterBean.radius, waterBean.yPosition + waterBean.radius);
    canvas.drawCircle(offset, waterBean.radius, paint);
    paint.color = Colors.black;
    canvas.drawPoints(ui.PointMode.points, [
      Offset(0,0),
      Offset(size.width,size.height),
//      Offset(100,200),
    ], paint);

    ui.ParagraphBuilder pb = ui.ParagraphBuilder(ui.ParagraphStyle(
      textAlign: TextAlign.center,
      fontWeight: FontWeight.w300,
      fontStyle: FontStyle.normal,
      fontSize: textSize,
    ));
    pb.pushStyle(ui.TextStyle(color: Colors.white));
    pb.addText('${waterBean.count}');

    ui.ParagraphConstraints pc = ui.ParagraphConstraints(width: waterBean.radius * 2);
//这里需要先layout, 后面才能获取到文字高度
    ui.Paragraph paragraph = pb.build()..layout(pc);
    canvas.drawParagraph(paragraph, Offset(waterBean.xPosition,waterBean.yPosition + waterBean.radius - textSize / 2));
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}

class WaterBean{
  double xPosition;
  double yPosition;
  double count;
  Color color;
  double transparency;
  double radius;
  int index;
  double textSize;
  Color textColor;
  WaterBean(this.textColor,this.index,this.textSize,this.xPosition, this.yPosition, this.count, this.color, this.radius);

}